package com.littlebee.storysuper.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.littlebee.storysuper.R;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class CircleGridAdapter extends BaseAdapter {

    private String[] mFiles;
    private LayoutInflater mLayoutInflater;
    private Context mContext;

    public CircleGridAdapter(String[] files, Context context) {
        this.mFiles = files;
        mLayoutInflater = LayoutInflater.from(context);
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mFiles == null ? 0 : mFiles.length;
    }

    @Override
    public String getItem(int position) {
        if (mFiles[position].startsWith("http%3A")) {
            return URLDecoder.decode(mFiles[position]);

        }
        return mFiles[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.item_gridview_circle, parent, false);
            holder.imageView = (ImageView) convertView.findViewById(R.id.album_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // 根据屏幕宽度动态设置图片宽高

        Glide.with(mContext).load(getItem(position)).error(R.drawable.corner_photo_default_img).into(holder.imageView);

        return convertView;
    }

    private static class ViewHolder {
        ImageView imageView;
    }
}
